﻿using Prism.Commands;
using Prism.Services.Dialogs; 
using SmartPlant.Model;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Telerik.Windows.Controls;

namespace SmartPlant.Core.Mvvm
{
    /// <summary>
    /// 对话框视图模型基类。
    /// </summary>
    public abstract class DialogViewModelBase : ViewModelBase, IDialogAware
    {
        private DialogPurpose diglogPurpose;

        public DialogPurpose DialogPurpose
        {
            get { return diglogPurpose; }
            set
            {
                diglogPurpose = value;
                RaisePropertyChanged();
            }
        }
        private DelegateCommand<string> _closeDialogCommand;
        private readonly IDialogService _dialogService;

        /// <summary>
        /// 默认构造函数。
        /// </summary>
        public DialogViewModelBase(IDialogService dialogService)
        {
            _dialogService = dialogService;
        }

        /// <summary>
        /// 获取系统的对话框服务。
        /// </summary>
        public IDialogService Dialog { get { return _dialogService; } }

        /// <summary>
        /// 设置指定的属性值，并触发属性值更改事件。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="storage"></param>
        /// <param name="value"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(storage, value))
            {
                return false;
            }
            storage = value;
            RaisePropertyChanged(propertyName);
            return true;
        }

        private string _title = "未命名对话框";
        /// <summary>
        /// 对话框标题
        /// </summary>
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        /// <summary>
        /// 对话框关闭事件。
        /// </summary>
        public event Action<IDialogResult> RequestClose;

        /// <summary>
        /// 对话框关闭命令。
        /// </summary>
        public ICommand CloseDialogCommand
        {
            get
            {
                if (_closeDialogCommand != null) return _closeDialogCommand;
                _closeDialogCommand = new DelegateCommand<string>(onCloseDialogCommand);
                return _closeDialogCommand;
            }
        }

        protected virtual void onCloseDialogCommand(string parameter)
        {
            DialogResult dialogResult = null;

            if (parameter?.ToLower() == "ok")
            {
                dialogResult = RaiseDialogOk();
            }
            else if (parameter?.ToLower() == "cancel")
            {
                dialogResult = RaiseDialogCancel();
            }
            else if (parameter?.ToLower() == "yes")
            {
                dialogResult = RaiseDialogYes();
            }
            else if (parameter?.ToLower() == "no")
            {
                dialogResult = RaiseDialogNo();
            }
            else if (parameter?.ToLower() == "none")
            {
                dialogResult = RaiseDialogNone();
            }
            RaiseRequestClose(dialogResult);
        }

        protected virtual DialogResult RaiseDialogOk()
        {
            return new DialogResult(ButtonResult.OK);
        }

        protected virtual DialogResult RaiseDialogCancel()
        {
            return new DialogResult(ButtonResult.Cancel);
        }

        protected virtual DialogResult RaiseDialogYes()
        {
            return new DialogResult(ButtonResult.Yes);
        }

        protected virtual DialogResult RaiseDialogNo()
        {
            return new DialogResult(ButtonResult.No);
        }

        protected virtual DialogResult RaiseDialogNone()
        {
            return new DialogResult(ButtonResult.None);
        }

        //触发窗体关闭事件
        public virtual void RaiseRequestClose(DialogResult dialogresult)
        {
            RequestClose?.Invoke(dialogresult);
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        /// <summary>
        /// 打开对话框，传入对话框参数。
        /// </summary>
        /// <param name="parameters"></param>
        public virtual void OnDialogOpened(IDialogParameters parameters)
        {
            Title = parameters.GetValue<string>(DialogParameterKeys.Title) ?? "";
            DialogPurpose = parameters.GetValue<DialogPurpose>(DialogParameterKeys.Purpose);
        }
    }
}
